/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.gui;

import app.gui.scene.SceneHolder;
import app.gui.scene.SceneObserver;
import com.app.game.control.HumanPlayer;
import com.app.game.other.Player;
import com.app.observers.HostListObserver;
import java.net.InetAddress;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.collections.ListChangeListener;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

/**
 *
 * @author sammael
 */
public class HostList extends SceneHolder implements HostListObserver, ListChangeListener<String> {

    private ListView<String> hostList;
    private SceneHolder joinHolder;
    private Button joinButton, refreshButton, backButton;
    private Player player;
    private TextField playerNameField;
    

    public HostList(SceneHolder previous) {
        previousHolder = previous;
        BorderPane border = new BorderPane();
        HBox vb = new HBox(spacing);
        vb.setAlignment(Pos.CENTER);
        vb.setPadding(new Insets(spacing, spacing / 2f, spacing / 2f, spacing / 2f));
        Label lbl = new Label("Imię gracza:");
        playerNameField = new TextField("Anonimowy gracz");
        HBox hb = new HBox(spacing);
        hb.setAlignment(Pos.CENTER);
        hb.getChildren().setAll(lbl, playerNameField);
        hb.setPadding(new Insets(spacing / 2f, spacing / 2f, spacing, spacing / 2f));
        joinButton = new Button("Dołącz");
        joinButton.setMaxWidth(buttonWidth);
        joinButton.setDisable(true);
        joinButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                if (joinHolder == null) {
                    joinHolder = new Room(HostList.this,new HumanPlayer(playerNameField.getText()),hostList.getSelectionModel().getSelectedItem());
                    for (SceneObserver ob : observers) {
                        joinHolder.registerObserver(ob);
                    }
                }
                nextScene = joinHolder.getScene();
                notifyObservers();
            }
        });
        refreshButton = new Button("Odśwież");
        refreshButton.setMaxWidth(buttonWidth);
        refreshButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                //synchronized (hostList) {
                hostList.getItems().clear();
                //}
            }
        });
        backButton = new Button("Powrót");
        backButton.setMaxWidth(buttonWidth);
        backButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                nextScene = previousHolder.getScene();
                notifyObservers();
            }
        });
        vb.getChildren().setAll(joinButton, refreshButton, backButton);
        hostList = new ListView<>();
        hostList.getSelectionModel().getSelectedItems().addListener(this);
        BorderPane hostBorder = new BorderPane();
        hostBorder.setCenter(hostList);
        hostBorder.setPadding(new Insets(0, spacing, 0, spacing));
        border.setCenter(hostBorder);
        border.setBottom(vb);
        border.setTop(hb);
        //border.setPadding(new Insets(spacing));

        scene = new Scene(border, rootWidth, rootHeigh);

    }

    @Override
    public void updateHost(final InetAddress host) {

        Platform.runLater(new Runnable() {

            @Override
            public void run() {
                if (!(hostList.getItems().contains(host.getHostAddress()))) {
                    hostList.getItems().add(host.getHostAddress());
                }

            }
        });

    }

    @Override
    public void onChanged(Change<? extends String> e) {
        if(hostList.getSelectionModel().getSelectedItem()!=null){
            joinButton.setDisable(false);
            
        }else{
            joinButton.setDisable(true);
        }
    }
}
